package com.btpj.lib_base.utils;

import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.util.Random;

/**
 * Created time  2021/3/13
 * @author : mcx
 * 类描述 : 
 */

public class NumberUtil {
    private static String[] hanArr = {"零", "一", "二", "三", "四", "五", "六", "七", "八", "九"};
    private static String[] unitArr = {"十", "百", "千", "万", "十", "白", "千", "亿", "十", "百", "千"};
    /**
     * String转成int的值， 若无法转换，默认返回0
     */
    public static int stoi(String string) {
        if (string!=null){
            string = string.trim();
        }
        return stoi(string, 0);
    }

    public static int stoi(String string, int defaultValue) {
        if ((string == null) || (string.equalsIgnoreCase(""))) {
            return defaultValue;
        }
        int id;
        try {
            id = Integer.parseInt(string);
        } catch (NumberFormatException e) {
            e.printStackTrace();
            return defaultValue;
        }
        return id;
    }

    /**
     * String转成long的值， 若无法转换，默认返回0
     */
    public static long stol(String string) {
        return stol(string, 0);
    }

    public static long stol(String string, long defaultValue) {
        if ((string == null) || (string.equalsIgnoreCase(""))) {
            return defaultValue;
        }
        long ret;
        try {
            ret = Long.parseLong(string);
        } catch (NumberFormatException e) {
            e.printStackTrace();
            return defaultValue;
        }

        return ret;
    }

    /**
     * String转成double的值， 若无法转换，默认返回0.00
     */
    public static double stod(String string) {
        return stod(string, 0.00);
    }

    public static double stod(String string, double defaultValue) {
        if ((string == null) || (string.equalsIgnoreCase(""))) {
            return defaultValue;
        }
        double ret;
        try {
            ret = Double.parseDouble(string);
        } catch (NumberFormatException e) {
            e.printStackTrace();
            return defaultValue;
        }

        return ret;
    }

    /**
     * 将整数转成中文表示
     */
    public static String toChineseNum(int number) {
        String numStr = String.valueOf(number);
        String result = "";
        int numLen = numStr.length();
        for (int i = 0; i < numLen; i++) {
            int num = numStr.charAt(i) - 48;
            if (i != numLen - 1 && num != 0) {
                result += hanArr[num] + unitArr[numLen - 2 - i];
                if (number >= 10 && number < 20) {
                    result = result.substring(1);
                }
            } else {
                if (!(number >= 10 && number % 10 == 0)) {
                    result += hanArr[num];
                }
            }
        }
        return result;
    }


    /**
     * 获取一个属于[min, max)中的随机数
     */
    public static int random(int min, int max) {
        return new Random().nextInt(max - min) + min;
    }

    /**
     * （1）四舍五入把double转化int整型，0.5进一，小于0.5不进一
     * @param number
     * @return
     */
    public static int getInt(double number){
        BigDecimal bd=new BigDecimal(number).setScale(0, BigDecimal.ROUND_HALF_UP);
        return Integer.parseInt(bd.toString());
    }


    /**
     * （2）四舍五入把double转化为int类型整数,0.5也舍去,0.51进一
     * @param dou
     * @return
     */
    public static int DoubleFormatInt(Double dou){
        DecimalFormat df = new DecimalFormat("######0"); //四色五入转换成整数
        return Integer.parseInt(df.format(dou));
    }


    /**
     * （3）去掉小数凑整:不管小数是多少，都进一
     * @param number
     * @return
     */
    public static int ceilInt(double number){
        return (int) Math.ceil(number);
    }



    /**
     * double类型如果小数点后为零显示整数否则保留
     * @param num
     * @return
     */
    public static String doubleTrans2(double num){
        if(Math.round(num)-num==0){
            return String.valueOf((long)num);
        }
        return String.valueOf(num);
    }


    /**
     * string 转double,double类型如果小数点后为零显示整数否则保留
     * @param num
     * @return
     */
    public static String string2doubleTransString(String num){
        double stod = stod(num);
        if(Math.round(stod)-stod==0){
            return String.valueOf((long)stod);
        }
        return String.valueOf(num);
    }

    /**
     * double转String,保留小数点后两位
     * @param num
     * @return
     */
    public static String doubleToString(double num){
        //使用0.00不足位补0，#.##仅保留有效位
        return new DecimalFormat("0.00").format(num);
    }

}
